package com.dongao.marketing.common.utils;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.support.PropertiesLoaderUtils;

/**
 * web层获取配置项工具类
 * @author guojipeng
 *
 */
public class ApiConfigUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(ApiConfigUtil.class);
	private static Properties props = new Properties();

	static {
		refershConfig(); // 先初始化一次
		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				refershConfig();
			}
		};

		Timer timer = new Timer();
		timer.schedule(task, new Date(), 1000 * 60 * 10);
	}

	public static void refershConfig() {
		try {
			props = PropertiesLoaderUtils.loadAllProperties("conf/ApiConfig.properties");
			
		} catch (IOException e) {
			logger.error(e.getMessage());
		}

	}

	public static String getStringValueByKey(String key) {
		Object value = props.get(key);
		if (value != null) {
			return value.toString();
		}
		return null;
	}

	public static String getStringValueByKey(String key, String defaultValue) {
		Object value = props.get(key);
		if (value == null) {
			return defaultValue;
		}
		return value.toString();
	}
	
}
